var emplid="";
var tourl="";
var tourlScrou="";
$(function(){
	var u="";
	var ua = navigator.userAgent;
	if(ua.indexOf("iPhone")>=0){
		u = window.location.search.replace("&amp;", "&");
	}else{
		u = window.pa_app.GetParam();
		if(u!="")
			u = "?"+u;
	}
	var url = u.split('?')[1];
	if(url!=null){
		var arr = url.split('&');
		for(var i = 0 ; i < arr.length;i++){
			var temp = arr[i];
			var tempArr = temp.split('=');
			var key = tempArr[0];
			if(key == "tourl"){
				tourl=tempArr[1];
			}
		}
		var len = u.split('?').length;
		if(len>1){
			tourl = tourl+"?"+u.split('?')[2];
			tourlScrou=tourl.split('?')[1];
			tourl = tourl.split('?')[0];
		}
	}
	
	//获取用户信息
	$.ajax({
		url:mpsurl+"/mobile/base/selectUserByEmpId.do",
		type:"post",
		timeout:180000,
		dataType:"json",
		success:function(data){
			var obj = eval(data);
			if(obj.resultCode=="500600"){
				emplid=obj.userDTO.emplUm;
				var phone=obj.userDTO.phone;
				$("#PhoneNumber").text(phone);
			}
			else{
				setPromptForInput(resultMessage);
			}
		},  
		error: function (xhr, textStatus, errorThrown) {
			setPromptForInput("网络异常，请检查网络后重试");
		}
	});
	
	var $login = $("#J-BtnExit"); 
	var $check = $("#huoquYZM");
	$login.on("tap", function(){
		/*
		//安卓关闭键盘
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
		}else{
			window.pa_app.closeKeyboard();
		}
		*/
		var checkCode = $("input[type=tel]").val().trim();  //验证码
		if(checkCode.length < 1){
			setPromptForInput("请输入手机收到的验证码，验证码可点击“获取验证码”来获取");
			return;
		}else{
			loginPrompt();
			$.ajax({
				url:mpsurl+"/mobile/otpYesKey/verifyTmpOTP.do",
				type:"post",
				timeout:180000,
				dataType:"json",
				data:{
					otp:checkCode
				},
				success:function(data){
					var obj1 = eval(data);
					if(obj1.resultCode=="500600"){
						//location.href=tourl;
						gotoLocation(tourl,tourlScrou);
					}else{
						$("#loginPromptBox").remove("slow");
						setPromptForInput((obj1.resultMessage!="")?obj1.resultMessage:"网络异常，请检查网络后重试");
					}
				},
				error: function (xhr, textStatus, errorThrown) {
					$("#loginPromptBox").remove("slow");
					setPromptForInput("网络异常，请检查网络后重试");
				}
			});
		}
	});

	//验证码效果(css)
	$check.one("tap", function(){
		getValidateCode();
	});
	function getValidateCode(){
		var PhoneNumber=$("#PhoneNumber").html();
		if(""==PhoneNumber||null==PhoneNumber){
			$check.one("tap", function(){ getValidateCode(); });
			setPromptForInput("您没有设置电话号码,不能获取验证码,请到PS系统设置");
		}else{
			$check.unbind("tap");
			$check.addClass("btn-check-disable");
			autoClosePrompt();
			var second = 60;
			$check.html(second);
			var time = setInterval(function(){
				second -= 1;
				$check.html(second);
				if(second == 0){
					$check.one("tap", function(){ getValidateCode(); });
					if($check.hasClass("btn-check-disable")){
						$check.removeClass("btn-check-disable");
					}
					$check.html("获取验证码");
					clearInterval(time);
				}
			}, 1000);
		}
	}
	//点击验证  提醒短信已发送
	function autoClosePrompt(){
		$.ajax({
			url:mpsurl+"/mobile/otpYesKey/generateTmpSMSOTP.do",
			type:"post",
			timeout:180000,
			dataType:"json",
			success:function(data){
				var obj = eval(data);
				if(obj.resultCode=="500600"){
					var yzm=obj.value;
					setPromptForInput("验证码已发送");
				}
				else{
					setPromptForInput((resultMessage!="")?resultMessage:"网络异常，请检查网络后重试");
				}
			},  
			error: function (xhr, textStatus, errorThrown) {
				setPromptForInput("网络异常，请检查网络后重试");
			}
		});
	    
	}
	
	$("#toHome").on("tap",function(){
		//安卓关闭键盘
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
		}else{
			window.pa_app.closeKeyboard();
		}
		setTimeout(function(){
			gotoLocation("/mobile/index.html","");
		},500);
	});
	$("#errorBtncancel").on("tap",function(){
		$("#GlobalMask").hide();
	});
});
/*
 * 登录提交跳转模态提示
 */
function loginPrompt(){
	var promptBox = "<div id='loginPromptBox' style='position:fixed;left:0;top:0;width:100%;height:100%;z-index:1000;display: block;'>"+
		"<div style='position:absolute;width:100%;height:100%;background-color:#000;opacity:.5;z-index:1000;'></div>"+
		"<div style='position: fixed;left: 30%;top: 40%;width: 40%;z-index: 1001;'>"+
		"<div style='position: absolute;width: 100%;height: 100%;background: #000;-webkit-border-radius: 8px;border-radius: 8px;opacity: .0;z-index: 1;'>"+
		"</div>"+
		"<div style='position: relative;display: -webkit-box;color: #fff;min-height:44px;-webkit-box-orient: horizontal;-webkit-box-pack: center;padding:28px 18px;font-size: 30px;text-align: center;z-index: 2;'>"+
		"<img src='images/loading.gif' width='70px' height='70px'/></div>"+
		"</div>"+
		"</div>";
	$("body").append(promptBox);
}